﻿using System;
using System.Linq;
using System.Windows;
using DiagramDesigner.BusinessModelObject;
using System.Windows.Controls;

namespace DiagramDesigner
{
    /// <summary>
    /// Interaction logic for AskConnection.xaml
    /// </summary>
    public partial class AskConnection
    {
        #region Constructor
        public AskConnection()
        {
            InitializeComponent();
        }
        #endregion

        #region Properties

        public BaseModel Item
        {
            get;
            set;
        }
        public DesignerCanvas Canvas
        {
            get; set;
        }
        public Image LinkImage
        {
            get { return image1; }
            set { image1 = value;}
        }
        public LinkModel LinkClass
        {
            get; set;
        }
        #endregion

        #region Events
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // realizando la conexion 
            if (ShowLink() == 1)
            {
                DialogResult = true;
                Close();
            }
            else
                Close();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ShowComboBox();
        }
        #endregion

        #region Methods
        Connection GetConnection(string source, string destination)
        {
            var iSource = GetDesignerItem(source);
            var iSink = GetDesignerItem(destination);

            var lados = new[] {"Left", "Top", "Right", "Bottom"};
            double distance = 0;
            Connector sourceConnector = null;
            Connector sinkConnector = null;


            for (var i = 0; i < lados.Count(); i++)
            {
                for (var j = 0; j < lados.Count(); j++)
                {
                    var sourceC = Canvas.GetConnector(iSource.ID, lados[j], Canvas);
                    var sink = Canvas.GetConnector(iSink.ID, lados[i], Canvas);

                    var longitud = Math.Sqrt(Math.Pow(sourceC.Position.X - sink.Position.X, 2) +
                                             Math.Pow(sourceC.Position.Y - sink.Position.Y, 2));
                    if((longitud<distance || distance==0 ) && (sourceC.Connections.Count==0 && sink.Connections.Count==0))
                    {
                        distance = longitud;
                        sourceConnector = sourceC;
                        sinkConnector = sink;
                    }
                }
            }
            if (sourceConnector != null)
            {
                LinkClass.StartElement = (MainElementModel)sourceConnector.ParentDesignerItem.Item;
                ((MainElementModel)sourceConnector.ParentDesignerItem.Item).SinkConnections.Add(LinkClass);
            }
            if (sinkConnector != null)
            {
                LinkClass.EndElement = (MainElementModel)sinkConnector.ParentDesignerItem.Item;
                ((MainElementModel)sinkConnector.ParentDesignerItem.Item).SourceConnections.Add(LinkClass);
            }
            var newCnn = new Connection(sourceConnector, sinkConnector, Item) {LinkClass = LinkClass};
            return newCnn;
        }

        int ShowLink()
        {
            if (comboBox1.Items.Count > 0 && comboBox2.Items.Count > 0)
            {
                if (comboBox1.SelectedIndex >= 0 && comboBox2.SelectedIndex >= 0)
                {
                    var nameSource = comboBox1.SelectedItem.ToString();
                    var nameDestination = comboBox2.SelectedItem.ToString();

                    if (nameDestination != nameSource)
                    {
                        var newConnection = GetConnection(nameSource, nameDestination);

                        Panel.SetZIndex(newConnection, Canvas.Children.Count);
                        Canvas.Children.Add(newConnection);
                        return 1;
                    }
                    else
                    {
                        MessageBox.Show("The Start Element and End Element cannot be the same");
                        return 0;
                    }
                }
                else                
                {
                    MessageBox.Show("Please, select the Start and End Elements");
                    return 0;
                }
            }
            else
            {
                MessageBox.Show("There aren't Elements");
                return 0;
            }
        }
        void ShowComboBox()
        {
            var designerItems = Canvas.Children.OfType<DesignerItem>();
            var enumerator = designerItems.GetEnumerator();
            while(enumerator.MoveNext())
            {
                if (enumerator.Current.Item != null)
                {
                    var item = ((MainElementModel)enumerator.Current.Item);
                    comboBox1.Items.Add(item.Name);
                    comboBox2.Items.Add(item.Name);
                }
            }
        }
        DesignerItem GetDesignerItem(string name)
        {
            var ie = Canvas.Children.OfType<DesignerItem>().Where(m => ((MainElementModel) m.Item).Name == name);
            var ge = ie.GetEnumerator();
            ge.MoveNext();
            var di = ge.Current;
            return di;
        }
        #endregion
    }
}
